package com.logistic.server.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.logistic.server.common.annotation.LoginRequired;
import com.logistic.server.common.utils.SecurityUtils;
import com.logistic.server.entity.common.PageRequest;
import com.logistic.server.entity.common.Result;
import com.logistic.server.entity.common.ResultGenerator;
import com.logistic.server.entity.pojo.SysRole;
import com.logistic.server.entity.pojo.SysUser;
import com.logistic.server.entity.pojo.SysUserRole;
import com.logistic.server.service.ISysRoleService;
import com.logistic.server.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息 前端控制器
 * </p>
 *
 * @author swen
 * @since 2021-10-25
 */
@Api(value = "角色信息接口", tags = {"角色信息接口"})
@Slf4j
@RestController
@LoginRequired
@RequestMapping("/system/role")
public class SysRoleController {

    @Resource
    private ISysRoleService roleService;

    @Resource
    private ISysUserService userService;

    /**
    * 角色信息列表
    * @param role 角色信息
    * @param pageRequest 分页参数
    * @return Result
    */
    @ApiOperation(value = "角色信息列表", notes = "角色信息列表")
    @GetMapping
    public Result<?> list(SysRole role, PageRequest pageRequest) {
        return ResultGenerator.genSuccessResult(roleService.selectPageRoleList(role, pageRequest));
     }

    /**
    * 角色信息新增
    * @param role 角色信息
    * @return Result
    */
    @ApiOperation(value = "角色信息新增", notes = "角色信息新增")
    @PostMapping
    public Result<?> add(@RequestBody SysRole role) {
        role.setCreateBy(SecurityUtils.getUsername());
        role.setCreateTime(new Date());
        roleService.save(role);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 角色信息删除
    * @param ids 角色信息主键
    * @return Result
    */
    @ApiOperation(value = "角色信息删除", notes = "角色信息删除")
    @DeleteMapping("/{ids}")
    public Result<?> delete(@PathVariable String ids) {
        roleService.removeByIds(Arrays.asList(ids.split(StringPool.COMMA)));
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 角色信息修改
    * @param role 角色信息
    * @return Result
    */
    @ApiOperation(value = "角色信息修改", notes = "角色信息修改")
    @PutMapping
    public Result<?> update(@RequestBody SysRole role) {
        role.setUpdateBy(SecurityUtils.getUsername());
        role.setUpdateTime(new Date());
        roleService.updateById(role);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 角色信息详情
    * @param id 角色信息主键
    * @return Result
    */
    @ApiOperation(value = "角色信息详情", notes = "角色信息详情")
    @GetMapping("/{id}")
    public Result<?> detail(@PathVariable String id) {
        return ResultGenerator.genSuccessResult(roleService.getById(id));
    }

    /**
     * 获取角色选择框列表
     */
    @ApiOperation("获取角色选择框列表")
    @GetMapping("/optionSelect")
    public Result<List<SysRole>> optionSelect() {
        return ResultGenerator.genSuccessResult(roleService.selectRoleAll());
    }

    /**
     * 查询已分配用户角色列表
     */
    @ApiOperation("查询已分配用户角色列表")
    @GetMapping("/authUser/allocatedList")
    public Result<?> allocatedList(SysUser user, PageRequest pageRequest) {
        return ResultGenerator.genSuccessResult(userService.selectAllocatedList(user, pageRequest));
    }

    /**
     * 查询未分配用户角色列表
     */
    @ApiOperation("查询未分配用户角色列表")
    @GetMapping("/authUser/unallocatedList")
    public Result<?> unallocatedList(SysUser user, PageRequest pageRequest) {
        return ResultGenerator.genSuccessResult(userService.selectUnallocatedList(user, pageRequest));
    }

    /**
     * 取消授权用户
     * @param userRole 用户权限
     * @return 结果
     */
    @ApiOperation("取消授权用户")
    @PutMapping("/authUser/cancel")
    public Result<?> cancelAuthUser(@RequestBody SysUserRole userRole) {
        roleService.deleteAuthUser(userRole);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 批量取消授权用户
     * @param roleId 角色id
     * @param userIds 用户ids
     * @return 结果
     */
    @ApiOperation("批量取消授权用户")
    @PutMapping("/authUser/cancelAll")
    public Result<?> cancelAuthUserAll(Long roleId, Long[] userIds) {
        roleService.deleteAuthUsers(roleId, userIds);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 批量选择用户授权
     * @param roleId 角色id
     * @param userIds 用户ids
     * @return 结果
     */
    @ApiOperation("批量选择用户授权")
    @PutMapping("/authUser/selectAll")
    public Result<?> selectAuthUserAll(Long roleId, Long[] userIds) {
        roleService.insertAuthUsers(roleId, userIds);
        return ResultGenerator.genSuccessResult();
    }
}
